<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Detail;
use App\Admin\Actions\Grid\User\AdminUser;
use App\Admin\Actions\Grid\User\Phone;
use App\Admin\Repositories\User;
use App\Enums\UserStatus;
use App\Models\Ding;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Support\Arr;

class UserController extends AdminController
{

    public bool $create = false;
    public bool $edit = false;
    public bool $delete = false;

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->model()
                ->with(['ding', 'department', 'adminUser.roles'])
                ->orderByDesc('status')
                ->orderByDesc('id');


            //$grid->column('id');
            $grid->column('department.name', trans('user.fields.ding_id') . '/' . trans('user.fields.dept_id'))->display(function () {
                return $this->ding->name . '/' . $this->department->name;
            });
            $grid->column('role', '角色')->display(function () {
                if (isset($this->adminUser)) {
                    return Arr::first($this->adminUser->roles)->name ?? '';
                }
                return '';
            });
            $grid->column('name');
            $grid->column('phone');
            $grid->column('avatar')->image('', '100', '100');

            $grid->column('status')->display(function ($value) {
                $description = UserStatus::getDescription($value);
                return "<span style='color: " . ($value ? 'green' : 'red') . "'>$description</span>";
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');
                $filter->equal('phone');
                $filter->equal('ding_id')->select(Ding::pluck('name', 'id'))->load('dept_id', '/getDept');
                $filter->equal('dept_id')->select();
                $filter->equal('status')->select(UserStatus::asSelectArray());

            });

            $grid->actions(function (Grid\Displayers\Actions $actions)  {
                if (Admin::user()->isAdministrator()) {
                    $actions->append(new Detail());
                } else {
                    $actions->disableDelete();
                }

                if ($actions->row->status == UserStatus::WORKING){
                    if (!$actions->row->admin_user_id) {
                        //如果再在职，并且未开通后台登录账号
                        $actions->append(new AdminUser());
                    }
                    //修改手机号
                    $actions->append(new Phone());
                }

            });

            $this->gridCommon($grid);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function detail($id): Show
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->model()->with(['department']);
            $show->field('id');
            $show->field('department_id', trans('user.fields.dept_id'))->as(function () {
                return $this->department->name;
            });
            $show->field('name');
            $show->field('phone');
            $show->field('avatar')->image('', 100, 100);
            $show->field('status')->as(function ($value) {
                return UserStatus::getDescription($value);
            });
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->text('name');
            $form->text('phone');
            $form->text('avatar');
            $form->text('userid');
            $form->text('unionid');
            $form->text('status');
        });
    }
}
